package com.henu.rescue.controller;

import com.henu.rescue.entity.Organization;
import com.henu.rescue.entity.Station;
import com.henu.rescue.entity.Supervise;
import com.henu.rescue.entity.Telephone;
import com.henu.rescue.entity.VO.TelVO;
import com.henu.rescue.service.OrgService;
import com.henu.rescue.service.StaService;
import com.henu.rescue.service.SuperviseService;
import com.henu.rescue.service.TelService;
import com.henu.rescue.utils.Page;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("tel/")
@Api("电话信息管理controller")
@Slf4j
public class TelController {
    private final TelService telService;

    @Autowired
    public TelController(TelService telService) {
        this.telService = telService;
    }

    @Autowired
    private StaService staService;

    @Autowired
    private OrgService orgService;

    @Autowired
    private SuperviseService superviseService;

    @ApiOperation("添加电话信息")
    @PostMapping("addTel")
    public Result addTel(@RequestBody Telephone telephone) {
        //不能直接添加组织和救援服务点的电话信息
        if(telephone.getTelType() == 1 || telephone.getTelType()== 2){
            return new Result(ResultEnums.PARM_ERROR, "没有权限");
        }
        Integer result = telService.addTel(telephone);
        if (result == 1) {
            return new Result(ResultEnums.SUCCESS, "电话信息添加成功！");
        } else {
            return new Result(ResultEnums.ADD_FAILED, "电话信息添加失败！");
        }
    }

    @ApiOperation("删除电话")
    @DeleteMapping("delTel/{telId}")
    public Result delTel(@PathVariable Integer telId) {
        Telephone telephone = telService.selectById(telId);
        if (telephone.getTelType() == 1 || telephone.getTelType() == 2) {
            return new Result(ResultEnums.PARM_ERROR, "没有权限");
        }
        Integer result = telService.delTel(telId);
        if (result == 1) {
            return new Result(ResultEnums.SUCCESS, "电话信息删除成功！");
        } else {
            return new Result(ResultEnums.DEL_ERROR, "电话信息删除失败！");
        }
    }

    @ApiOperation("修改电话")
    @PostMapping("updateTel")
    public Result updateTel(@RequestBody Telephone telephone) {
        if(telephone.getTelType() == 1 || telephone.getTelType()== 2){
            return new Result(ResultEnums.PARM_ERROR, "没有权限");
        }
        Integer result = telService.updateTel(telephone);
        if (result == 1) {
            return new Result(ResultEnums.SUCCESS, "电话信息修改成功！");
        } else {
            return new Result(ResultEnums.DEL_ERROR, "电话信息修改失败！");
        }
    }

    @ApiOperation("分页查询")
    @PostMapping("getAllByPage")
    public Result<Page<TelVO>> getAllByPage(@RequestBody Page<TelVO> page){
        //先从station和orgnation中获取到电话号码 然后写入到数据库
        //orgnation电话 type：1
        List<Organization> allOrg = orgService.getAllOrg();
        //System.out.println(allOrg.size());
        for (int i = 0; i < allOrg.size(); i++) {
            Organization organization = allOrg.get(i);
            String orgTel = organization.getOrgTel();
            Telephone telephone = new Telephone();
            telephone.setTelType(1);
            telephone.setTelNumber(orgTel);
            telephone.setHostId(organization.getOrgId());
            //如果当前电话信息没有就新增上
            Integer count = telService.selectTel(telephone.getTelNumber(), telephone.getTelType(), telephone.getHostId());
            if(count == 0) {
                telService.addTel(telephone);
            }
        }
        //station type:2
        List<Station> stations = staService.selectAll();
        //System.out.println(stations.size());
        for (int i = 0; i < stations.size(); i++) {
            Station station = stations.get(i);
            Telephone telephone = new Telephone();
            telephone.setTelType(2);
            telephone.setTelNumber(station.getStaTel());
            telephone.setHostId(station.getStaId());
            Integer count = telService.selectTel(telephone.getTelNumber(), telephone.getTelType(), telephone.getHostId());
            if(count == 0){
                telService.addTel(telephone);
            }
        }
        //查询业务
        page.setSortMethod("desc");
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"sta_id"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page= telService.selectTelInfoByPage(page);
        //将tel信息加工成前端tel视图
        List<TelVO> telephones = page.getList();
        ArrayList<TelVO> telVOS = new ArrayList<>();
        for (int i = 0; i < telephones.size(); i++) {
            TelVO telVO = new TelVO();
            BeanUtils.copyProperties(telephones.get(i),telVO);
            if(telVO.getTelType() == 0){
                telVO.setType("监管部门");
                String name = superviseService.selectNameById(telVO.getHostId());
                telVO.setName(name);
            }else if(telVO.getTelType() == 1){
                telVO.setType("救援服务组织部门");
                String name = orgService.getNameById(telVO.getHostId());
                telVO.setName(name);
            }else if(telVO.getTelType() == 2){
                telVO.setType("救援服务点");
                String name = staService.getNameById(telVO.getHostId());
                telVO.setName(name);
            }
            telVOS.add(telVO);
        }
        page.setList(telVOS);
        return new Result(ResultEnums.QUERY_SUCCESS,page);
    }

    @ApiOperation("获取所有的监管部门")
    @PostMapping("getAllSupervise")
    public Result getAllSupervise(){
        List<Supervise> supervises = superviseService.selectAll();
        return new Result(ResultEnums.QUERY_SUCCESS,supervises);
    }
}
